package com.after.saas.user.controller;


import com.after.saas.common.annotation.Log;
import com.after.saas.common.api.service.ExternUserService;
import com.after.saas.common.constants.Result;
import com.after.saas.common.model.query.UserQuery;
import com.after.saas.common.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Api(tags = "登录管理")
@RestController
public class LoginController {

    @Autowired
    private ExternUserService externUserService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${jwt.encode.secret}")
    private String secret;

    /**
     * 授权登录
     *
     * @param userVO
     * @return
     */
    @Log(value = "登入")
    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserQuery userVO) {
        return ResponseUtil.SUCCESS(externUserService.login(userVO));
    }

    @Log(value = "注销")
    @ApiOperation("注销")
    @GetMapping(value = "/logout")
    public Result logout() {
        String token = RequestUtil.getToken();
        String userId = JwtUtil.getUserIdFromToken(token, secret);
        LoginUtil.userLogout(redisTemplate, userId);
        return ResponseUtil.SUCCESS();
    }
}
